﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using HCMessage.Model;

namespace HCBusiness
{
    public class ReportHandler
    {


        public ItemReport UpdateItemReport()
        {
            var result = new ItemReport();
            var env = new HCData.EnvironmentCategory();
            var report = new HCData.ReportHandler();
            result.EnvCtList = env.GetEnvironmentGategoryList();
            result.ItemReportList = env.GetItemReportDetail();
            var guid = Guid.NewGuid();
            foreach (var r in result.ItemReportList)
            {
                r.Guid = guid.ToString();
                CheckHttpResponse(r);
                report.InsertItemReport(r);
            }
            return result;
        }

        public void  CheckHttpResponse(ItemReportDetail entity)
        {
            string result;
            try
            {
                var request = (HttpWebRequest)WebRequest.Create(entity.Address);
                request.Timeout = 100;
                var response = (HttpWebResponse)request.GetResponse();
                if (request.HaveResponse)
                {
                    entity.Desc = "success";
                    entity.Status = "Pass"; 
                }
                else
                {
                    entity.Desc = "no response";
                    entity.Status = "Fail";
                }
                response.Close();
              
            }
            catch(Exception ex)
            {
                entity.Desc = ex.ToString();
                entity.Status = "Fail";
            }

        }


    }
}
